package com.jic.point.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.jic.common.base.vo.RestResult;
import com.jic.point.constant.consist.ResponseCode;
import com.jic.point.model.entity.SettSupplierRule;
import com.jic.point.model.entity.SettSupplierRuleApply;
import com.jic.point.model.vo.request.SettSupplierRuleApplyRequest;
import com.jic.point.model.vo.response.SettSupplierRuleApplyResponse;
import com.jic.point.service.SettSupplierRuleApplyService;
import com.jic.point.service.SettSupplierRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: tzc
 * @date: 2020-10-11 17:28:33
 */
 @RestController
 @Slf4j
 @RequestMapping("/settSupplierRuleApply")
 @Api(tags = " [供应商结算规则申请表]控制类-PC")
public class SettSupplierRuleApplyController {
	@Autowired
	private SettSupplierRuleApplyService service;
    @Autowired
    private SettSupplierRuleService ruleService;
	
     /**
      *新增记录
      * @param request 新增[供应商结算规则]请求参数
      * @return SettSupplierRuleResponse 新增[供应商结算规则]响应参数
      * author tzc
      * @date 2020-3-11 20:54:04
      */
    @ApiOperation(value = "新增[供应商结算规则]记录")
    @PostMapping("/save")
    @ResponseBody
    public RestResult<Long> save(@RequestBody SettSupplierRuleApplyRequest request) throws Throwable {
        SettSupplierRuleApply settSupplierRule = new SettSupplierRuleApply();
    		BeanUtils.copyProperties(request, settSupplierRule);
    	    long flag = service.addSupplierRuleApply(settSupplierRule);
     return RestResult.success(flag);
    }
	
     /**
     /**
      * 新增或修改记录
      * @param request 新增或修改[供应商结算规则]请求参数
      * @return SettSupplierRuleResponse 新增或修改[供应商结算规则]响应参数
      * author tzc
      * @date 2020-3-11 20:54:04
      */
     @ApiOperation(value = "保存或更新[供应商结算规则]")
     @PostMapping("/saveOrUpdate")
      @ResponseBody
     public RestResult<Long> saveOrUpdate(@Validated @RequestBody SettSupplierRuleApplyRequest request) throws Throwable {
         SettSupplierRuleApply settSupplierRule = new SettSupplierRuleApply();
		     BeanUtils.copyProperties(request, settSupplierRule);
	         long flag =   service.saveOrUpdate(settSupplierRule);
      return RestResult.success(flag);
     }
	
     /**
      * 删除记录
      * @param request 删除[供应商结算规则]请求参数
      * @return SettSupplierRuleResponse 删除[供应商结算规则]响应参数
      * author tzc
      * @date 2020-3-11 20:54:04
      */
     @ApiOperation(value = "删除[供应商结算规则]记录")
     @PostMapping("/delete")
     @ResponseBody
     public RestResult<Long> delete(@RequestBody SettSupplierRuleApplyRequest request) throws Throwable {
         SettSupplierRuleApply settSupplierRule = new SettSupplierRuleApply();
	     	BeanUtils.copyProperties(request, settSupplierRule);
	         long flag = service.deleteByPrimaryKeyLogically(settSupplierRule);
      return RestResult.success(flag);
     }
	
     /**
      * 修改记录
      * @param request 修改[供应商结算规则]请求参数
      * @return SettSupplierRuleResponse 修改[供应商结算规则]响应参数
      * author tzc
      * @date 2020-3-11 20:54:04
      */
     @ApiOperation(value = "修改[供应商结算规则]记录")
     @PostMapping("/update")
     @ResponseBody
     public RestResult<Long> update(@RequestBody SettSupplierRuleApplyRequest request) throws Throwable {
         SettSupplierRuleApply settSupplierRule = new SettSupplierRuleApply();
	     	BeanUtils.copyProperties(request, settSupplierRule);
	         long flag = service.updateByPrimaryKeySelective(settSupplierRule);
      return RestResult.success(flag);
     }
	
     /**
      * 单表查询详情信息
      * @param request 查询[供应商结算规则]详情请求参数
      * @return SettSupplierRuleResponse 查询[供应商结算规则]详情响应参数
      * author tzc
      * @date 2020-3-11 20:54:04
      */
      @PostMapping("/getByPrimaryKey")
      @ApiOperation(value = "查询[供应商结算规则]详情")
      @ResponseBody
     public RestResult<SettSupplierRuleApplyResponse> getByPrimaryKey(@Validated  @RequestBody SettSupplierRuleApplyRequest request) {
     	log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
	     if (request!=null && request.getId()!=null) {

             SettSupplierRuleApply settSupplierRule = new SettSupplierRuleApply();
	     	BeanUtils.copyProperties(request, settSupplierRule);
             SettSupplierRuleApply result = service.getByPrimaryKey(settSupplierRule);
		     log.info("查询详情请求参数出参，{}", JSONObject.toJSONString(result));
		     if (result == null) {
		     	return RestResult.error(ResponseCode.EXCEPTION, "记录不存在");
		     }
             SettSupplierRuleApplyResponse bo = new SettSupplierRuleApplyResponse();
		     BeanUtils.copyProperties(result, bo);
	     	return RestResult.success(bo);
	     }
     	log.info("查询详情请求结束");
	     return RestResult.success(new SettSupplierRuleApplyResponse());
     }
	
    /**
      * 分页查询
      *
      * @param request 分页查询请求参数
      * @return 分页列表
      * @throws Throwable 业务异常和系统异常
      */
     @PostMapping("/listPage")
     @ApiOperation(value = " 分页查询列表 ")
     @ResponseBody
     public RestResult<PageInfo<SettSupplierRuleApply>> listPage (@RequestBody SettSupplierRuleApplyRequest request) throws Throwable {
         SettSupplierRuleApply param = new SettSupplierRuleApply();
      BeanUtils.copyProperties(request, param);
      param.setDeleteFlag(0);
      param.setSts(1);
     return RestResult.success(service.listPageBySelective(param, request.getPageNum(),request.getPageSize()));
     }
     
	
      /**
       *SETT_SUPPLIER_RULE 查询列表（不分页）
       * @param  request 前端请求参数
       */
      @PostMapping("/listAll")
      @ApiOperation(value = "不分页查询 [供应商结算规则] ")
      @ResponseBody
      public RestResult<List<SettSupplierRuleApplyResponse>>  listSettSupplierRuleAllRecord(@RequestBody SettSupplierRuleApplyRequest request) throws Throwable {
          SettSupplierRuleApply param = new SettSupplierRuleApply();
	     	BeanUtils.copyProperties(request, param);
	     	param.setDeleteFlag(0);
	     	List<SettSupplierRuleApply> list = service.listAllRecord(param);
			List<SettSupplierRuleApplyResponse> responseList = list.stream().map(temp -> {
                SettSupplierRuleApplyResponse bo = new SettSupplierRuleApplyResponse();
			BeanUtils.copyProperties(temp, bo);
			return bo;
			}).filter(item -> item != null).collect(Collectors.toList());
		    return RestResult.success(responseList);
      }

    /**
     * 待审核供应商规则查询
     */
    @PostMapping("/audit")
    @ApiOperation(value = "待审核[供应商结算规则] ")
    @ResponseBody
    public  RestResult<PageInfo<SettSupplierRuleApply>> audit (@RequestBody SettSupplierRuleApplyRequest request) throws Throwable {
        SettSupplierRuleApply param = new SettSupplierRuleApply();
        BeanUtils.copyProperties(request, param);
        param.setDeleteFlag(0);

        return RestResult.success(service.listPageBySelective(param, request.getPageNum(),request.getPageSize()));
    }
    /**
     * 审核提交供应商规则
     */
    @ApiOperation(value = "审核提交[供应商结算规则]")
    @PostMapping("/auditSubmit")
    @ResponseBody
    public RestResult<Long> auditSubmit(@RequestBody SettSupplierRuleApplyRequest request) throws Throwable {
        SettSupplierRuleApply settSupplierRule = new SettSupplierRuleApply();
        BeanUtils.copyProperties(request, settSupplierRule);
        settSupplierRule.setUpdater(request.getAdminOperate().getOperator());
        settSupplierRule.setAuditorId(request.getAdminOperate().getOperator());
        if(settSupplierRule.getSts()==2){
            settSupplierRule.setDeleteFlag(0);
            long flag = service.updateByPrimaryKeySelective(settSupplierRule);
            if(flag!=1){
                return RestResult.error(ResponseCode.EXCEPTION,"审核拒绝失败");
            }
          return RestResult.success(1L);
        }
        long flag = service.updateByPrimaryKeySelective(settSupplierRule);
        if(flag<1){

            return RestResult.error(ResponseCode.EXCEPTION,"失败");
        }else{
            SettSupplierRuleApply byPrimaryKey = service.getByPrimaryKey(settSupplierRule);
            byPrimaryKey.setId(null);
            SettSupplierRule rule=new SettSupplierRule();
            BeanUtils.copyProperties(byPrimaryKey,rule);
            ruleService.insertSelective(rule);
            SettSupplierRuleApply applyRequest =new SettSupplierRuleApply();
            applyRequest.setDeleteFlag(1);
            applyRequest.setId(request.getId());
            service.updateByPrimaryKeySelective(applyRequest);
        }
        return RestResult.success(flag);
    }

}

	
